package util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public class Anagrama {
	private List<String> anagramas= new ArrayList<String>(); 
	private List<String> validas = new ArrayList<String>();
	private InputStream is;
	private int numpalavras,numPalavrasValidas;
	private HashSet<String> tabela = new HashSet<String>();
	
	
	public void setIs(InputStream is) {
		this.is = is;
	}
	/**
	public int numPalavras(String word){
		return fatorial(word.length());
	}
	*/
	public void criaAnagramas(String prefix, String word) {  
		//anagramas = 
		//validas = new ArrayList<String>(anagramas.size());
		
		if (word.length() <= 1) {  
            anagramas.add(prefix + word);  
        } else {  
            for (int i = 0; i < word.length(); i++) {  
                String cur = word.substring(i, i + 1);  
                String before = word.substring(0, i);   
                String after = word.substring(i + 1);   
                criaAnagramas(prefix + cur, before + after);  
            }  
        }
       
    }
	
	public List<String> criaValidas() {
		//BufferedReader bf = abreArquivo("vocabulario.txt");
		BufferedReader bf = arraydebits(is);
		String str;
		// int i=0;
		 try {
			 while ((str = bf.readLine()) != null) {
			     if(anagramas.contains(str))
			    	 validas.add(str);
			    	//i++; 
			     //System.out.println(str);
			    }
			//System.out.println(i);
			 bf.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//numPalavrasValidas = 2;
		return validas;
		
	}
	public BufferedReader arraydebits(InputStream is) {
		BufferedReader buff = null;
		//InputStream is = new FileInputStream(fNome);
		buff = new BufferedReader(new InputStreamReader(is));
		
		return buff;
		
	}
	public void cria(String palavra) {
		criaAnagramas("", palavra);
		
	}
	/**
	 * @return the numpalavras
	 */
	public int getNumpalavras() {
		return numpalavras;
	}
	
	/**
	 * @return the numPalavrasValidas
	 */
	public int getNumPalavrasValidas() {
		return numPalavrasValidas;
	}
	
	public List<String> validas() {
		return validas;
	}
	public void val() {
		BufferedReader bf = arraydebits(is);
		String str;
		try {
			bf.mark(8000);
			 while ((str = bf.readLine()) != null){
				 tabela.add(str);
			 }
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void criaValidas2(){
		
		for(String srt:anagramas){
			if(tabela.contains(srt)) validas.add(srt);
		}
		
	}
	
}
